package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.EmployeeQueryObject;
import cn.wolfcode.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

//员工控制器
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    //查询    /employee/list.do
    @RequestMapping("/list")
    //value能传递多个参数,同时设置权限表达式和权限的名称,修改他们的默认关系为或
    @RequiresPermissions(value = {"employee:list", "员工列表"} , logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("departments", departmentService.listAll());
        model.addAttribute("pageResult", pageResult);
        return "employee/list";
    }

    //删除    /employee/delete.do?id=?
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        employeeService.deleteById(id);
        return new JsonResult();
    }

    //修改或者新增
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() != null) {
            employeeService.update(employee , ids);
        } else {
            employeeService.insert(employee , ids);
        }
        return new JsonResult();
    }

    //跳转新增和修改页面    /employee/input.do
    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //  查询所有的部门的数据传给浏览器
        model.addAttribute("departments", departmentService.listAll());
        //  查询所有的角色的数据传给浏览器
        model.addAttribute("roles", roleService.listAll());
        //  如果请求里有id则是修改操作根据id查询该条数据共享给编辑页面
        if (id != null) {
            model.addAttribute("employee", employeeService.selectById(id));
        }
        return "employee/input";
    }

    // 批量删除    /employee/batchDelete.do?ids=2&ids=3&ids=17...
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        employeeService.batchDelete(ids);
        return new JsonResult();
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap<String,Boolean> checkName(String name, Long id){
        // 插件判断返回结果需要为键值对形式,key为valid,value为boolean类型，定义一个map封装
        HashMap<String, Boolean> map = new HashMap<>();
        // 有id表示是在编辑
        if (id != null) {
            // 如果新名字和旧名字一样则直接允许修改
            if (name.equals(employeeService.selectById(id).getName())) {
                map.put("valid", true);
                return map;
            }
        }
        // 如果新名字和旧名字不一样，则去查询验证有没有同名的
        Employee employee = employeeService.selectByName(name);

        map.put("valid",employee==null);
        return map;
    }

    //导出
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开,第三个参数设置下载的名字）
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //接收业务层返回的excel文件
        Workbook wb = employeeService.exportXls();
        //通过输出流把excel文件输出给浏览器
        wb.write(response.getOutputStream());
    }
    //导入
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return new JsonResult();
    }


}
